package testing;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import bot.FileStorage;

public class AddTwelveEstimates {
	
	public static void main(String[] args){
		File estimates = new File("c:\\Users\\Jamison\\Documents\\PopulationInfo\\Maine2Estimates12.txt");
		File data = new File("c:\\Users\\Jamison\\Documents\\PopulationInfo\\Maine2Data.txt");
		File output = new File("c:\\Users\\Jamison\\Documents\\PopulationInfo\\TempData.txt");
		
		try {
			BufferedReader dataReader = new BufferedReader(new FileReader(data));
			BufferedReader estReader = new BufferedReader(new FileReader(estimates));
			BufferedWriter writer = new BufferedWriter(new FileWriter(output));
			
			List<String[]> estEntries = new ArrayList<String[]>();
			String line = estReader.readLine();
			while (line != null){
				estEntries.add(line.split(","));
				line = estReader.readLine();
			}
			
			line = dataReader.readLine();
			while (line != null){
//				String entry[] = line.split(FileStorage.split);
//				String sumlev = entry[35];
//				String state = entry[36];
//				String place = entry[37];
//				String name = entry[1];
//				String estValue = "null";
//				
//				for (String[] est: estEntries){
//					if (sumlev.equals(est[0]) && state.equals(est[1]) && place.equals(est[3])){
//						estValue = est[12];
//						break;
//					}
//				}
//				
//				writer.write(line + FileStorage.split + estValue);
//				writer.write("\n");
//				line = dataReader.readLine();
				
				//towns
				String entry[] = line.split(FileStorage.split);
				String name = entry[1];
				String estValue = "null";
				String sumlev = "null";
				String state = "null";
				String place = "null";
				
				for (String[] est: estEntries){
					if (name.equals(est[6].substring(0, est[6].length() - 5)) 
							&& "town".equals(est[6].substring(est[6].length() - 4, est[6].length()))){
						estValue = est[12];
						sumlev = est[0];
						state = est[1];
						place = est[3];
//						System.out.println(est[6] + est[12]);
					}
				}
				
				//get rid of null sumlev, state, and place
				StringBuilder builder = new StringBuilder(1000);
				for (int i = 0; i < entry.length - 3; i++){
					builder.append(entry[i]);
					builder.append(FileStorage.split);
				}
				builder.append(sumlev);
				builder.append(FileStorage.split);
				builder.append(state);
				builder.append(FileStorage.split);
				builder.append(place);
				builder.append(FileStorage.split);
				builder.append(estValue);
				
				writer.write(builder.toString());
				writer.write("\n");
				line = dataReader.readLine();
			}
			
			
			dataReader.close();
			estReader.close();
			writer.close();
		} 
		catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
	}
}
